package com.example.batch.config;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RocketMQConfig {
    
    @Value("${rocketmq.name-server}")
    private String nameServer;

    @Value("${rocketmq.producer.group}")
    private String producerGroup;

    // 修改RocketMQConfig.java中的Producer配置
    @Bean
    public RocketMQTemplate rocketMQTemplate() {
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(nameServer);

        // 增加超时时间和重试机制
        producer.setSendMsgTimeout(10000);  // 发送超时时间从3秒调整为10秒
        producer.setRetryTimesWhenSendFailed(3);  // 失败重试次数

        RocketMQTemplate template = new RocketMQTemplate();
        template.setProducer(producer);
        return template;
    }

}